function $(selector){
    return document.querySelector(selector);
}
function $$(selector){
    return document.querySelectorAll(selector);
}
function createtab(){//创建棋盘
    for(var i=0;i<14;i++){
        var tr=document.createElement('tr');
        for(var j=0;j<14;j++){
            var td=document.createElement('td');
            td.setAttribute('data-line',j);
            td.setAttribute('data-column',i);
            tr.appendChild(td);
        }
        tab.appendChild(tr);
    }
}

function createchess(chess){
    if(!findchess(chess)&&!gamesovered){
        allchess.push(chess);
        var div=document.createElement("div");
        if(chess.x<14&&chess.y<14)//左上
        {
            var td=$(`td[data-line='${chess.x}'][data-column='${chess.y}']`);
            div.className=`lefttop ${chess.z}`;
        }else if(chess.x===14&&chess.y<14)//右上
        {
            var td=$(`td[data-line='13'][data-column='${chess.y}']`);
            div.className=`righttop ${chess.z}`;
        }
        else if(chess.x<14&&chess.y===14)//左下
        {
            var td=$(`td[data-line='${chess.x}'][data-column='13']`);
            div.className=`leftbottom ${chess.z}`;
        }
        else if(chess.x===14&&chess.y===14)//右下
        {
            var td=$(`td[data-line='13'][data-column='13']`);
            div.className=`rightbottom ${chess.z}`;
        }
        div.setAttribute('data-line',`${chess.x}`);//将属性给棋子，防止后面查找时找不到边上的棋子
        div.setAttribute('data-column',`${chess.y}`);
        td.appendChild(div);
        whichone=whichone==='white'?'black':'white';
        check();
    }
}
function findchess(chess){
    var result=allchess.find(function(item){
      return item.x===chess.x && item.y===chess.y;
    })
    return result===undefined?false:true;
  }
// 检查游戏是否结束
function check(){
    for(var i=0;i<allchess.length;i++){
        var curchess=allchess[i];
        var chess1,chess2,chess3,chess4;
        // 横着
        chess1=allchess.find(function(node){
            return node.x===curchess.x+1 && node.y===curchess.y&&node.z===curchess.z;
        });
        chess2=allchess.find(function(node){
            return node.x===curchess.x+2 && node.y===curchess.y&&node.z===curchess.z;
        });
        chess3=allchess.find(function(node){
            return node.x===curchess.x+3 && node.y===curchess.y&&node.z===curchess.z;
        });
        chess4=allchess.find(function(node){
            return node.x===curchess.x+4 && node.y===curchess.y&&node.z===curchess.z;
        });
        if(chess1&&chess2&&chess3&&chess4)
        {
            end(curchess,chess1,chess2,chess3,chess4);
            return;
        }
        //竖着
        chess1=allchess.find(function(node){
            return node.x===curchess.x && node.y===curchess.y+1&&node.z===curchess.z;
        });
        chess2=allchess.find(function(node){
            return node.x===curchess.x && node.y===curchess.y+2&&node.z===curchess.z;
        });
        chess3=allchess.find(function(node){
            return node.x===curchess.x && node.y===curchess.y+3&&node.z===curchess.z;
        });
        chess4=allchess.find(function(node){
            return node.x===curchess.x && node.y===curchess.y+4&&node.z===curchess.z;
        });
        if(chess1&&chess2&&chess3&&chess4)
        {
            end(curchess,chess1,chess2,chess3,chess4);
            return;
        }
        //斜着
        chess1=allchess.find(function(node){
            return node.x===curchess.x+1 && node.y===curchess.y+1&&node.z===curchess.z;
        });
        chess2=allchess.find(function(node){
            return node.x===curchess.x+2 && node.y===curchess.y+2&&node.z===curchess.z;
        });
        chess3=allchess.find(function(node){
            return node.x===curchess.x+3 && node.y===curchess.y+3&&node.z===curchess.z;
        });
        chess4=allchess.find(function(node){
            return node.x===curchess.x+4 && node.y===curchess.y+4&&node.z===curchess.z;
        });
        if(chess1&&chess2&&chess3&&chess4)
        {
            end(curchess,chess1,chess2,chess3,chess4);
            return;
        }
        //第二种斜
        chess1=allchess.find(function(node){
            return node.x===curchess.x-1 && node.y===curchess.y+1&&node.z===curchess.z;
        });
        chess2=allchess.find(function(node){
            return node.x===curchess.x-2 && node.y===curchess.y+2&&node.z===curchess.z;
        });
        chess3=allchess.find(function(node){
            return node.x===curchess.x-3 && node.y===curchess.y+3&&node.z===curchess.z;
        });
        chess4=allchess.find(function(node){
            return node.x===curchess.x-4 && node.y===curchess.y+4&&node.z===curchess.z;
        });
        if(chess1&&chess2&&chess3&&chess4)
        {
            end(curchess,chess1,chess2,chess3,chess4);
            return;
        }
    } 
}
function end(){
    if(!gamesovered){
    for(var i=0;i<allchess.length;i++){
       $(`div[data-line='${allchess[i].x}'][data-column='${allchess[i].y}']`).innerHTML=i+1;
    }
    for(i=0;i<arguments.length;i++){
        $(`div[data-line='${arguments[i].x}'][data-column='${arguments[i].y}']`).classList.add('win');
    }
    gamesovered=true;
   }
}

function main(){
    createtab();
    tab.addEventListener('click',function(e){
        if(!gamesovered){
            var temp=e.target.dataset;//使用data-xxx定义
            if(e.target.tagName==='TD'){
                var left=e.offsetX;//水平位置
                var top=e.offsetY;//垂直位置
                var width=tab.clientWidth*0.92/14;//td边长
                var positionx=left>width/2;
                var positiony=top>width/2;
                var chess={
                 x:positionx?parseInt(temp.line)+1:parseInt(temp.line),
                 y:positiony?parseInt(temp.column)+1:parseInt(temp.column),
                 z:whichone,
                }
                createchess(chess);//创建棋子
            }
        }
        else{
            if(confirm("游戏结束是否重开一局？")){
                whichone='white';
                allchess=[];
                gamesovered=false;
                tab.innerHTML='';
                createtab();
            }
        }
    });
}
var tab=$('.tablecontainer');
var whichone='white';
var allchess=[];
var gamesovered=false;
main();